package ligne2vie.topos.server.service;

import static org.junit.Assert.*;

import java.util.List;

import ligne2vie.topos.shared.bo.TSummit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * The class {@link FindSummitAroundTestCase} verifies if the class {@link SummitServiceImpl} correctly works in an
 * independent way from the Web Interface, when the user is logged in.
 * 
 * @author thuguerre
 * @since 1.1.0
 */
public class FindSummitAroundTestCase extends AbstractSummitServiceTestCase {

	@Before
	public void setUp() {

		super.setUp();
		userLogin(false);
	}

	@After
	public void tearDown() {
		super.tearDown();
	}

	/**
	 * Inserting 2 summits with a certain distance. Looking for exactly one of them.
	 */
	@Test
	public void oneExactSummit() {

		// --- supposed to be found summit
		service.createSummit("0/0", 1, 0, 0, "");

		// --- rejected summit
		service.createSummit("10/10", 1, 10, 10, "");

		// --- looking for the summit
		List<TSummit> summits = service.findSummitsAround(0, 0, 25);

		// --- verification

		assertNotNull(summits);
		assertEquals(1, summits.size());

		assertEquals("0/0", summits.get(0).getName());
		assertEquals(0, summits.get(0).getLatitude(), 0);
		assertEquals(0, summits.get(0).getLongitude(), 0);
	}

	/**
	 * Inserting 5 summits with a certain distance. Looking for exactly 3 of them.
	 */
	@Test
	public void severalSummits() {

		// --- supposed to be found summits
		service.createSummit("0/0", 1, 0, 0, "");
		service.createSummit("1/0", 1, 1, 0, "");
		service.createSummit("0/1", 1, 0, 1, "");

		// --- rejected summits
		service.createSummit("1.5/1.5", 1, 1.5, 1.5, "");
		service.createSummit("10/10", 1, 10, 10, "");

		// --- looking for the summits
		// 1 degree ~ 110 km. All expected summits are at 1 degree max
		List<TSummit> summits = service.findSummitsAround(0, 0, 115);

		// --- verification

		assertNotNull(summits);
		assertEquals(3, summits.size());

		for (TSummit summit : summits) {

			assertTrue(summit.getName().equals("0/0") || summit.getName().equals("1/0")
					|| summit.getName().equals("0/1"));
		}
	}
}
